<?
include "server_config.php";
?>
<list><?
	/*if(!eregi($HTTP_HOST,$HTTP_REFERER)) {
		exit;
	}*/
	$key = $_GET['key'];
	$base = $_GET['base'];
	$findword = false;
	$step = 20;
	if(preg_match("/^(ㄱ)$/",$key)) {
		$pattern = "name REGEXP '^(ㄱ)' OR ( name >= '가' AND name < '까' )";
	} elseif(preg_match("/^(ㄲ)$/",$key)) {
		$pattern = "name REGEXP '^(ㄲ)' OR ( name >= '까' AND name < '나' )";
	} elseif(preg_match("/^(ㄴ)$/",$key)) {
		$pattern = "name REGEXP '^(ㄴ)' OR ( name >= '나' AND name < '다' )";
	} elseif(preg_match("/^(ㄷ)$/",$key)) {
		$pattern = "name REGEXP '^(ㄷ)' OR ( name >= '다' AND name < '따' )";
	} elseif(preg_match("/^(ㄸ)$/",$key)) {
		$pattern = "name REGEXP '^(ㄸ)' OR ( name >= '따' AND name < '라' )";
	} elseif(preg_match("/^(ㄹ)$/",$key)) {
		$pattern = "name REGEXP '^(ㄹ)' OR ( name >= '라' AND name < '마' )";
	} elseif(preg_match("/^(ㅁ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅁ)' OR ( name >= '마' AND name < '바' )";
	} elseif(preg_match("/^(ㅂ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅂ)' OR ( name >= '바' AND name < '빠' )";
	} elseif(preg_match("/^(ㅃ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅃ)' OR ( name >= '빠' AND name < '사' )";
	} elseif(preg_match("/^(ㅅ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅅ)' OR ( name >= '사' AND name < '싸' )";
	} elseif(preg_match("/^(ㅆ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅆ)' OR ( name >= '싸' AND name < '아' )";
	} elseif(preg_match("/^(ㅇ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅇ)' OR ( name >= '아' AND name < '자' )";
	} elseif(preg_match("/^(ㅈ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅈ)' OR ( name >= '자' AND name < '짜' )";
	} elseif(preg_match("/^(ㅉ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅉ)' OR ( name >= '짜' AND name < '차' )";
	} elseif(preg_match("/^(ㅊ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅊ)' OR ( name >= '차' AND name < '카' )";
	} elseif(preg_match("/^(ㅋ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅋ)' OR ( name >= '카' AND name < '파' )";
	} elseif(preg_match("/^(ㅍ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅍ)' OR ( name >= '파' AND name < '타' )";
	} elseif(preg_match("/^(ㅌ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅌ)' OR ( name >= '타' AND name < '하' )";
	} elseif(preg_match("/^(ㅎ)$/",$key)) {
		$pattern = "name REGEXP '^(ㅎ)' OR ( name >= '하' AND name <= '힣' )";
	} else{
		$pattern = "name REGEXP '.+(".$key.").*'";
		$hpattern = "name REGEXP '^(".$key.").*'";
		$findword = true;
	}

	if ($findword)
	{
		$sql = "
			SELECT COUNT(*) FROM ndd_hospital WHERE $hpattern
		";
		$result = mysql_query($sql,$connect);
		$row = mysql_fetch_row($result);
		$hcount = $row[0];
	
		$sql = "
			SELECT count(*) FROM ndd_hospital WHERE $pattern
		";
		$result = mysql_query($sql,$connect);
		$row = mysql_fetch_row($result);
		$count = $row[0];

		//echo "count:".$count." hcount:".$hcount." base:".$base;
		if ($hcount > $base) // 검색어로 시작 존재
		{
			$sql = "
				SELECT name, address, phone, hospital_idx FROM ndd_hospital WHERE $hpattern ORDER BY name ASC LIMIT $base,$step
			";

			$result = mysql_query($sql,$connect);

			while($row = mysql_fetch_row($result))
			{
				$name = htmlentities($row[0], ENT_QUOTES | ENT_IGNORE, "UTF-8");
				$address = htmlentities($row[1], ENT_QUOTES | ENT_IGNORE, "UTF-8");
				$phone = $row[2];
				$id = $row[3];
				echo "<hospital><name>".$name."</name><address>".$address.
					"</address><phone>".$phone."</phone><id>".$id."</id><image>img/default.png</image></hospital>";
			}
			if ($hcount - $base < $step) // 검색어 시작이 step이 안될 때
			{
				$number = $step - $hcount + $base;
				$sql = "
					SELECT name, address, phone, hospital_idx FROM ndd_hospital WHERE $pattern ORDER BY name ASC LIMIT 0,$number
				";

				$result = mysql_query($sql,$connect);

				while($row = mysql_fetch_row($result))
				{
					$name = htmlentities($row[0], ENT_QUOTES | ENT_IGNORE, "UTF-8");
					$address = htmlentities($row[1], ENT_QUOTES | ENT_IGNORE, "UTF-8");
					$phone = $row[2];
					$id = $row[3];
					echo "<hospital><name>".$name."</name><address>".$address.
						"</address><phone>".$phone."</phone><id>".$id."</id><image>img/default.png</image></hospital>";
				}
			}

		}
		else if ($hcount + $count > $base)
		{
			$cbase = $base - $hcount;
			$sql = "
				SELECT name, address, phone, hospital_idx FROM ndd_hospital WHERE $pattern ORDER BY name ASC LIMIT $cbase,$step
			";

			$result = mysql_query($sql,$connect);

			while ($row = mysql_fetch_row($result))
			{
				$name = htmlentities($row[0], ENT_QUOTES | ENT_IGNORE, "UTF-8");
				$address = htmlentities($row[1], ENT_QUOTES | ENT_IGNORE, "UTF-8");
				$phone = $row[2];
				$id = $row[3];
				echo "<hospital><name>".$name."</name><address>".$address.
					"</address><phone>".$phone."</phone><id>".$id."</id><image>img/default.png</image></hospital>";
			}
		}
		$morecount = $hcount + $count - $base - $step;
	}
	else
	{
		$sql = "
			SELECT count(*) FROM ndd_hospital WHERE $pattern
		";
		$result = mysql_query($sql,$connect);
		$row = mysql_fetch_row($result);
		$count = $row[0];

		$sql = "
			SELECT name, address, phone, hospital_idx FROM ndd_hospital WHERE $pattern ORDER BY name ASC LIMIT $base,30
		";

		$result = mysql_query($sql,$connect);

		while($row = mysql_fetch_row($result))
		{
			$name = htmlentities($row[0], ENT_QUOTES | ENT_IGNORE, "UTF-8");
			$address = htmlentities($row[1], ENT_QUOTES | ENT_IGNORE, "UTF-8");
			$phone = $row[2];
			$id = $row[3];
			echo "<hospital><name>".$name."</name><address>".$address.
				"</address><phone>".$phone."</phone><id>".$id."</id><image>img/default.png</image></hospital>";
		}
		$morecount = $count - $base - $step;
	}
	echo "<more>".($morecount)."</more>"; ?>
</list>
